package main

func main() {

	//strsArray := []string{"a", "b", "c"}
	//str := "sssssss"

	//测试用例
	/* haystack := "mississippi"
	needle := "issip" */
	haystack := "hello"
	needle := "ll"

	//打印测试用例返回结果
	resp := strStr(haystack, needle)
	println(resp)

}

func strStr(haystack string, needle string) int {
	if needle == "" && haystack == "" {
		return 0
	}
	if needle == "" {
		return 0
	}
	if haystack == "" {
		return -1
	}
	haystackLen := len(haystack)
	needleLen := len(needle)
	if needleLen > haystackLen {
		return -1
	}
	needlePtr := 0
	pos := -1
	//count := 0 //记录相等的个数
	for i := 0; i < haystackLen; i++ {

		if haystack[i:i+1] == needle[needlePtr:needlePtr+1] {
			pos = i
			if needlePtr+1 == needleLen {
				pos = pos - needleLen + 1
				return pos
			} else {
				needlePtr++
			}
		} else {
			if needlePtr != 0 { //回溯到最原始的i+1
				i = i - needlePtr
			}
			needlePtr = 0
		}
	}
	return -1
}
